//###############
//FILE : NameWantServersSession.java
//WRITER : Daniel Lowengrub, daniel_i_l, 320687098
//EXERCISE : oop ex3 2011
//DESCRIPTION:
//See below
//###############

package oop.ex3.protocol.sessions;

import oop.ex3.protocol.messages.WantServersMsg;
import oop.ex3.protocol.messages.ContainNameServerMsg;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.ArrayList;

import oop.ex3.communication.SocketAddress;
import oop.ex3.exceptions.InvalidMessageException;

/**
 * Send the list of NS that we know about.
 * @author daniel
 */
public class NameWantServersSession extends NameToFileSession {
    private ArrayList<SocketAddress> _nameServers;

    /**
	 * Create a new session.
	 */
    public NameWantServersSession() {
        super(new WantServersMsg());
    }

    /**
     * Set the list of NSs we know about.
     * @param nameServers The NS list.
     */
    public void setNameServers(ArrayList<SocketAddress> nameServers) {
        _nameServers = nameServers;
    }

    @Override
    protected boolean specExecuteSession(DataInputStream inputStream, DataOutputStream outputStream) throws IOException, InvalidMessageException {
        WantServersMsg wantServers = new WantServersMsg();
        wantServers.receive(inputStream);

        //if we received the request then send the list
        for (SocketAddress nameServer : _nameServers) {
            ContainNameServerMsg containServer = new ContainNameServerMsg();
            containServer.setIp(nameServer.getIp());
            containServer.setPort(nameServer.getPort());
            containServer.send(outputStream);
        }

        _endList.send(outputStream);

        return true;
    }

}
